package com.iye.puretennis.utils;

import com.iye.puretennis.helper.Constant;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2014/12/5
 * Time: 9:47
 */
public class TimeUtils {

    public static final String TIME_SEPARATOR = "-";

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd";


    public static String dateLabel(String date) {

        Calendar calendar = Calendar.getInstance();

        int day = calendar.get(Calendar.DAY_OF_MONTH);

        String inputDayStr = date.substring(8, 10);

        int inputDay = Integer.parseInt(inputDayStr);

        int amount = inputDay - day;

        String prefix = getDayOfWeek(date);

        if (amount == 0) {
            prefix = Constant.TODAY_TEXT;
        } else if (amount == 1) {
            prefix = Constant.TOMORROW_TEXT;
        } else if (amount == 2) {
            prefix = Constant.DAY_AFTER_TOMORROW_TEXT;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
            try {
                Date d = sdf.parse(date);
                calendar.setTime(d);
                int month = calendar.get(Calendar.MONTH) + 1;
                int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                date = formatTime(month) + Constant.MONTH_SUFFIX + formatTime(dayOfMonth) + Constant.DAY_SUFFIX;
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date + "," + prefix;
        }

        return prefix;
    }


    public static String formatTime(int t) {
        return t >= 10 ? "" + t : "0" + t;//三元运算符 t>10时取 ""+t
    }


    public static String today() {
        Calendar calendar = Calendar.getInstance();

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);


        return year + TIME_SEPARATOR + formatTime(month) + TIME_SEPARATOR + formatTime(day);
    }

    public static String now() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return sdf.format(calendar.getTime());
    }


    /**
     * @param currentDataIndex the page index
     * @param days             the days every page
     * @param next             if has next page
     * @return
     */
    public static String getDate(int currentDataIndex, int days, boolean next) {
        Calendar calendar = Calendar.getInstance();
        if (next) {
            calendar.add(Calendar.DAY_OF_MONTH, currentDataIndex * days);
        } else {
            calendar.add(Calendar.DAY_OF_MONTH, -currentDataIndex * days);
        }
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        return year + TIME_SEPARATOR + formatTime(month) + TIME_SEPARATOR + formatTime(day);

    }

    public static int getTotalDaysAmountIn2Week() {

        Calendar calendar = Calendar.getInstance();

        return calendar.get(Calendar.DAY_OF_WEEK) + 8;

    }

    public static String plusDays(String currentDay, int days) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        Calendar calendar = Calendar.getInstance();
        try {
            Date date = sdf.parse(currentDay);
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, days);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(calendar.getTime());
    }


    public static String getFirstDayOfWeek(int index) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String currentMonday = sdf.format(calendar.getTime());
        return plusDays(currentMonday, index * 7);
    }

    public static String getDayOfWeek(String currentDay) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        Calendar calendar = Calendar.getInstance();
        try {
            Date date = sdf.parse(currentDay);
            calendar.setTime(date);
            return getDayOfWeek(calendar.get(Calendar.DAY_OF_WEEK) - 2);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getDayOfWeek(int position) {
        switch (position) {
            case 0:
                return "星期一";
            case 1:
                return "星期二";
            case 2:
                return "星期三";
            case 3:
                return "星期四";
            case 4:
                return "星期五";
            case 5:
                return "星期六";
            default:
                return "星期日";

        }

    }

    public static String getPlayYear(int year, int month) {
        Calendar calendar = Calendar.getInstance();

        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        int monthsBetween = (currentYear - year) * 12 + (currentMonth - month);

        int years = monthsBetween / 12;
        int months = monthsBetween % 12;
        StringBuilder builder = new StringBuilder();
        if (years == 0) {
            return builder.append(months).append("个月").toString();
        }
        if (months == 0) {
            return builder.append(years).append("年").toString();
        }
        return builder.append(years).append("年零").append(months).append("个月").toString();
    }

    public static int getAppointmentTimeMinute(int minute) {
        if (minute > 0 && minute <= 15) {
            minute = 1;
        } else if (minute > 15 && minute <= 30) {
            minute = 2;
        } else if (minute > 30 && minute <= 45) {
            minute = 3;
        } else {
            minute = 0;
        }
        return minute;
    }

    public static int getAppointmentTimeHour(int hour, int minute) {
        if (minute > 45) {
            hour++;
        }
        return hour;
    }

    public static boolean isToday(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        Calendar calendar = Calendar.getInstance();
        try {
            Date currentDate = sdf.parse(date);
            return currentDate.getDate() == calendar.get(Calendar.DAY_OF_MONTH) && currentDate.getMonth() == calendar.get(Calendar.MONTH) && currentDate.getYear() + 1900 == calendar.get(Calendar.YEAR);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }


}
